<template>
  <form-dialop
    :title="$t('desktopManage.enterDesktopInformation')"
    :visible="visible"
    width="30%"
    @handleClose="handleClose"
		v-loading="loading"
  >
    <template #content>
      <div style="padding-top: 25px;">
        <el-form ref="form" :model="formData" label-position="right" :rules="rule" label-width="110px">
          <el-form-item :label="$t('desktopManage.tenantName')" prop="tenant_name" name="tenant_name">
            <el-input v-model="formData.tenant_name" :placeholder="$t('desktopManage.inputTenantName')" @keyup.enter.native="submit"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <template #footer>
      <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="submit" :disabled="loading">{{ $t('common.confirm') }}</el-button>
    </template>
  </form-dialop>
</template>
<script lang="ts">
import Vue from 'vue'
import FormDialop from '@/components/Dialog/formDialog.vue';
import { AppModule } from '@/store/modules/app'
export default Vue.extend({
  components: {
    FormDialop
  },
  props: {
    createMethod: {
      type: Function,
      default() {
        return () => Promise.resolve();
      }
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      formData: {
        tenant_name: ''
      },
      language: AppModule.language,
      rule: {
        tenant_name: [
          { required: true, message: this.$t('message.tenantName'), trigger: 'blur' },
          { max: 50, message:  this.$t('message.nameLengthRule'), trigger: 'blur' },
        ]
      },
      desktopData: ''
    }
  },
  methods: {
    init(row: any) {
      this.desktopData = JSON.parse(JSON.stringify(row));
      this.formData.tenant_name = "";
      this.$nextTick(() => {
        if (this.$refs['form']) {
          (this.$refs['form'] as any).clearValidate();
        }
      })
      this.visible = true;
    },
    submit() {
      (this.$refs.form as any).validate((valid: any) => {
        if (valid) {
          this.loading = true;
          const data = {
            cloud_tenant_name: this.formData.tenant_name,
            resource_id: (this as any).desktopData.resource_id,
            auth_id: (this as any).desktopData.occupied_user.database.auth_id || (this as any).desktopData.occupied_user.vmware.auth_id ,
          }
          const headers = {
            'Content-Type':'application/json'
          }
          this.$tunnyApi.apiPostInputDesktopInfo(data, headers).then((res: any) => {
            if(res.uuid && res.cloud_tenant_id) {
              const data = {
                cloud_tenant_id: res.cloud_tenant_id,
                ad_account_uuid: res.uuid
              }
              this.$pandaApi.apiUpdateCloudTenant((this as any).desktopData.id, data).then((res: any) => {
                // console.log(res)
                switch(res.code) {
                  case 400:
                    this.$message.warning((this as any).$t("desktopManage.enterDesktopInformationFail"));
                    break;
                  case 200:
                    this.$emit("success")
                    this.$message.success((this as any).$t("desktopManage.enterDesktopInformationSuccess"))
                    this.visible = false;
                    break;
                }
              }).catch(() => {
                this.$message.warning((this as any).$t("desktopManage.enterDesktopInformationFail"))
              })
            }
            switch(res.code) {
              case 400:
                this.$message.warning((this as any).$t("desktopManage.enterDesktopInformationFail"))
                break;
              case 10041:
                this.$message.warning((this as any).$t("desktopManage.tenantNotExists"));
                break;
              case 10054:
                this.$message.warning((this as any).$t("desktopManage.adNotExists"));
                break;
              case 10052:
                this.$message.warning((this as any).$t("desktopManage.adAccountNotExists"));
                break;
            }
            this.loading = false;
          }).catch(() => {
            this.$message.warning((this as any).$t("desktopManage.enterDesktopInformationFail"))
            this.loading = false;
          })
        }
      });
    },
    handleClose() {
      this.visible = false;
			this.loading = false;
      this.formData.tenant_name = ""
    }
  }
})
</script>
<style scoped>
</style>
